(function(angular) {
	"use strict";
	angular.module("fs.waf").controller("cls.rollover.logController", logController);
	logController.$inject = [ "$scope", "$q", "modalService", "gridService", "layoutConfig", "validatorConfig", "authService", "repaymentService", "rolloverService" ];
	function logController($scope, $q, modalService, gridService, layoutConfig, validatorConfig, authService, repaymentService, rolloverService) {
		var ctrl = this, params = $scope.$stateParams.params, errorMessage = {};
		ctrl.validatorConfig = validatorConfig;
		ctrl.repayment = {};
		ctrl.actionName = "paymentLog";
		ctrl.layoutConfig = layoutConfig;
		ctrl.init = function() {
		};

		ctrl.finalRepayFlagChanged = function(repayment) {
			repaymentService.finalRepayFlagChanged(repayment);
		};

		ctrl.repayAmtChanged = function(repayment) {
			repaymentService.repayAmtChanged(repayment);
		};

		ctrl.payPrincAmtChanged = function(repayment) {
			repaymentService.payPrincAmtChanged(repayment);
		};

		ctrl.repayTypeChanged = function(repayment) {
			repaymentService.repayTypeChanged(repayment, ctrl.layoutConfig.formConfigs.general.gridConfigs);
		};
		ctrl.save = function() {
			var messages = [];
			angular.forEach(errorMessage, function(field) {
				angular.forEach(field, function(rule) {
					messages.push(rule);
				});
			});
			if (messages.length > 0) {
				modalService.openErrorDialogs({
					"messages" : messages
				});
			} else {
				return authService.logSubmitted(ctrl.repayment).then(submitSuccessful, submitFailed);
			}
		};

		function submitSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				"messages" : [ "title.dialog.successful.submit" ],
				"btnCallback" : {
					"close" : function() {
						$scope.go("cls.workflow.wfTxn.list");
					}
				},
				"redirections" : [ {
					"name" : "title.infobar.workflow.list",
					"state" : "cls.workflow.wfTxn.list",
					"params" : {}
				} ]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				"messages" : response.error
			});
		}

		function selectLoanId(param) {
			if (!param || param.length == 0) {
				modalService.openErrorDialogs({
					messages : [ "title.dialogs.info.norecsel" ]
				});
				return false;
			}

			authService.logInit(ctrl.repayment).then(function(response) {
				return $q(function(resoved, rejected) {
					resoved(response);
				});
			}, submitFailed).then(function(response) {
				var repayment = response.data;
				var defer1 = $q.defer(), defer2 = $q.defer();
				repaymentService.getRolloverLayout(repayment.loan.loanInfo.prodSubtype, repayment.loan.loanInfo.prodType, "log").then(function(layout) {
					defer1.resolve(layout);
				});
				rolloverService.getRolloverProdCodeOptions({
					prodCode : repayment.rolloverProdCode
				}).then(function(options) {
					defer2.resolve(options);
				});
				$q.all({
					layout : defer1.promise,
					options : defer2.promise
				}).then(function(result) {
					ctrl.layoutConfig = result.layout;
					ctrl.repayment = response.data;
					ctrl.repayment.repayType = "R";
					ctrl.repayment.rolloverProdCodeOptions = result.options;
					ctrl.repayment.payType_Show = false;
					ctrl.actionName = "paymentCreateInit";
				});
			});
			$scope.disableUnloadCheck();
		}

		ctrl.loanIdLookUp = {
			"id" : "cls.lookupTxnRefList",
			"params" : {
				"loanNo" : "<loanNo",
				"lockFlag" : "N",
				"prodType" : "STD",
				"loanStatusList" : [ "N" ]
			},
			"size" : [ 1144 ],
			"boundTo" : {
				"loanId" : "loanId"
			},
			"callback" : {
				"ok" : selectLoanId
			},
			"manualInput" : false,
			"okEvent" : true,
			"cancelEvent" : false
		};

	}
})(angular);
